<?php

class IndexController extends Site_Controller_Action
{
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/css/custom-theme/jquery-ui-1.10.2.custom.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
                
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/index.js');
        
        $query = 'SELECT SQL_CACHE BAN_ID,BAN_DSC,BAN_LNK FROM FULLBANNER WHERE BAN_STA = 1 ORDER BY BAN_ORD ASC';
        $fullbanner = $this->db->fetchAll($query);  
        $this->view->fullbanner = $fullbanner;
        
        $query = 'SELECT SQL_CACHE QEX_ID, QEX_NOM FROM QUEIXA WHERE QEX_STA = 1 ORDER BY QEX_NOM ASC';
        $queixa = $this->db->fetchAll($query);  
        $this->view->queixa = $queixa;
        
        if($this->getRequest()->isPost()) {
            try {
                $formData = $this->getRequest()->getPost();
                //print_r($formData);
                
                $data = array(
                    'AGA_NOM'    => $formData['nome'],
                    'AGA_TEL'    => $formData['telefone'],
                    'AGA_EML'    => $formData['email'],
                    'AGA_CID'    => $formData['cidade'],
                    'AGA_QEX'    => $formData['queixa'],
                    'AGA_DAT'    => date('Y-m-d'),
                );
                
                $int = $this->db->insert('AGENDA_AVALIACAO', $data);
                
                
                Zend_Loader::loadClass('envioemail','library');
                $arr['queixa']  = $formData['queixa'];
                $arr['nome']    = $formData['nome'];
                $arr['email']   = $formData['email'];
                $arr['telefone']= $formData['telefone'];
                $arr['cidade']  = $formData['cidade'];
                $arr['urlbase'] = $this->_urlbase;
                $ok = envioemail::avaliacao($arr);
                
                
                echo "<script>alert('Agenda enviada com sucesso')</script>";
            } catch (Exception $exc) {
                //echo 'error: '.$exc->getMessage();
                echo "<script>alert('Não foi possível enviar a agenda')</script>";
            }     
        }

    }
}

